using DevExpress.XtraEditors;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using  WinformDevelopFrame.Core.Extend;
using  WinformDevelopFrame.IServices;
using  WinformDevelopFrame.Entity;
using DevExpress.XtraEditors.Repository;
using WinformDevelopFrame.Common;
using WinformDevelopFrame.Core.Form;
namespace  WinformDevelopFrame.UI
{
    public partial class Frmsysuserlog : BaseForm
    {
        #region 自动生成代码
        private ISqlSugarClient _sqlSugarClient;
        private IsysuserlogService _sysuserlogService;
        private ISysDataTableService _sysDataTableService;
        private IsysmenuService _sysmenuService;
        public  Frmsysuserlog(ISqlSugarClient sqlSugarClient, IsysuserlogService sysuserlogService,IsysmenuService sysmenuService,ISysDataTableService sysDataTableService)
        {
            _sqlSugarClient = sqlSugarClient;
            _sysuserlogService = sysuserlogService;
            _sysmenuService = sysmenuService;
            _sysDataTableService = sysDataTableService;
            InitializeComponent();
            Init();
        }

        public override void Form_Load()
        {
            InitListCol();
            xtraTabControl1.TabPages.Remove(xtraTabPage2);

        }
        /// <summary>
        /// 设置list lookupedit 列
        /// </summary>
        private void InitListCol()
        {
            UserId.Properties.NullText = "";
            gridView1.Columns["UserId"].ColumnEdit = new RepositoryItemLookUpEdit()
            {
                Name = "repositoryItemLookUpEditUserId",
                DataSource = _sqlSugarClient.Ado.GetDataTable(_sysDataTableService
                .QueryByClause(p => p.DataTableName.Equals("用户-lookupedit")).SQL),
                DisplayMember = "用户名称",
                ValueMember="Id"
            };
            
        }
        //设置不可编辑
        private void SetReadOnlyField()
        {
        }
        /// <summary>
        /// 设置数据 设置列名
        /// </summary>
        private void Init()
        {
            fileds.Add("IP", "IP");
            fileds.Add("UserId", "用户");
            fileds.Add("Form", "业务界面");
            fileds.Add("Action", "行为");
            fileds.Add("ViewTime", "时间");
            SetReadOnlyField();
            var m = _sysmenuService.QueryByClause(p => p.MenuCode.Equals(this.Name));
            //获取工具按钮
            var menus=_sysmenuService.QueryListByClause(p=>p.PId==m.Id).OrderBy(p=>p.OrderNo).ToList();
            menus.ForEach(p =>
            {
                btns.Add(new SimpleButton()
                {
                    Text = p.MenuText,
                    Name = p.MenuCode,
                    Image = Image.FromFile(Application.StartupPath+p.MenuIcon)
                });
            });
            UserId.Properties.DataSource = _sqlSugarClient.Ado.GetDataTable(_sysDataTableService
                .QueryByClause(p => p.DataTableName.Equals("用户-lookupedit")).SQL);
            UserId.Properties.DisplayMember = "用户名称";
            UserId.Properties.ValueMember = "Id";
        }
        public override void SetGridData()
        {
            gridControl1.DataSource = _sysuserlogService.Query();
        }
        /// <summary>
        /// 数据校验
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            return true;
        }
        public override void AddFunction()
        {
        }
        public override void GridControlDoubleClickFunction()
        {
            ID = ((sysuserlog)gridView1.GetRow(gridView1.FocusedRowHandle)).Id;
        }
        public override void EditFunction()
        {
        }

        public override bool SaveFunction()
        {
            if (CheckData())
            {
                if(FormStatus==FormStatus.Add)
                {
                    ID=_sysuserlogService.Insert(this.ControlDataToModel<sysuserlog>());
                    return ID>0;
                }
                else
                {
                    return _sysuserlogService.Update(this.ControlDataToModel<sysuserlog>());
                }
            }

            return false;
        }

        public override bool DeleteFunction()
        {
            return _sysuserlogService.DeleteById(ID);
        }

        public override bool CancelFunction()
        {
            return base.CancelFunction();
        }
        public override void InitToolBtnStatusFunction(FormStatus status, SimpleButton btn)
        {
            base.InitToolBtnStatusFunction(status,btn);
        }

        public virtual void SetToolBtnFunction()
        {

        }
        #endregion
    }
}